<?php
/**
 * @Author: rocky
 * @Copyright: 广州拓冠科技 <http://my8m.com>
 * Date: 2019/8/29
 * Time: 10:00
 */


namespace app\shop\controller\api\v1;


use app\common\controller\BaseApiController;
use app\common\facade\Token;
use app\common\model\ShopMemberIdentity;
use app\common\model\ShopMessageTip;
use app\common\model\ShopStudyCate;
use app\common\model\User;
use app\common\tools\Arr;
use app\shop\validate\MemberValidate;
use app\common\model\ShopUserMoneyRecord;
use app\common\model\PcasArea;
use app\shop\model\ShopMemberAddress;

/**
 * 会员管理
 * Class Member
 * @package app\shop\controller\api\v1
 */
class Member extends BaseApiController
{
    
    //获取（多条）用户收货地址
    public function getUserAddress(){
        $uid = $this->request->param('uid');
        $res = ShopMemberAddress::where('uid',$uid)->select();
        $this->successCode($res);
    }
    //获取（单条）用户收货地址
    public function getFindAddress(){
        $lid = $this->request->param('Lid');
        $res = ShopMemberAddress::where('id',$lid)->find();
        //查询省市区id,通过街道id查询3个父级id
        // $selectData = ShopMemberAddress::getParentIds($res['address_code']);
        // usort($selectData, function ($a, $b) {
        //   return $a['level'] - $b['level'];
        // });
        $data=[];
        $data['addressData'] = $res;
        // $data['selectData'] = $selectData;
        
        $this->successCode($data);
    }
    
    //获取默认地址
    public function getDetailedAdrs(){
        $uid = $this->request->param('uid');
        // $lid = $this->request->param('lid',0);
        $res = ShopMemberAddress::where('uid',$uid)->where('is_default',1)->find();
        if($res == null){
            $res01 = ShopMemberAddress::where('uid',$uid)->order('id','desc')->find();
            $this->successCode($res01);
        }
        $this->successCode($res);
    }
    
    //添加收货地址
    public function postAddress(){
        $type = $this->request->param('type');
        $id = $this->request->param('id');
        $uid = $this->request->param('uid');//用户id
        $username = $this->request->param('username');//联系人
        $phone = $this->request->param('phone');//电话号码
		$latitude = $this->request->param('latitude');//纬度
		$longitude = $this->request->param('longitude');//经度
		$address = $this->request->param('address');//地址
        $d_address = $this->request->param('d_address');//详细地址
        $addressDoor = $this->request->param('address_door');//门牌号
        $is_default = $this->request->param('is_default');//是否默认地址
        $det_action = ShopMemberAddress::where('uid',$uid)->count();
        // halt($det_action);
        if($det_action == 0){
            $data = [
                'uid'=>$uid,
                'name'=>$username,
                'phone'=>$phone,
                'latitude'=>$latitude,
                'longitude'=>$longitude,
                'address'=>$address,
                'detailed_address'=>$d_address,
                'address_door'=>$addressDoor,
                'is_default'=>1
            ];
        }else{
            $data = [
                'uid'=>$uid,
                'name'=>$username,
                'phone'=>$phone,
                'latitude'=>$latitude,
                'longitude'=>$longitude,
                'address'=>$address,
                'detailed_address'=>$d_address,
                'address_door'=>$addressDoor,
                'is_default'=>$is_default
            ];
        }
        // 判断是否为编辑，1是编辑
        if($type == 1){
            ShopMemberAddress::where('uid',$uid)->update(['is_default'=>0]);
            $res = ShopMemberAddress::where('id',$id)->update($data);
            $this->successCode($res);
        }else{
            ShopMemberAddress::where('uid',$uid)->update(['is_default'=>0]);
            $res = ShopMemberAddress::insert($data);
            $this->successCode($res);
        }
        $this->errorCode(100,'获取失败');
        
    }
    
    //删除收货地址
    public function postDelAddress(){
        $id = $this->request->param('id');
        $res = ShopMemberAddress::where('id',$id)->delete();
        if($res == 1){
            $this->successCode($res);
        }else{
            $this->errorCode(100,'获取失败');
        }
    }
    
    //获取省市区街道地址数据
    public function getArea (){
        $id = $this->request->param('select_id',0);
        $level = $this->request->param('select_level',1);
        $res = PcasArea::where('level',$level)->where('parent_id',$id)->select();
        $this->successCode($res);
    }
    
    
    //添加信息
    public function postInfo(){
        $data = (new MemberValidate())->goCheck();
        $data['id'] = Token::id();
        $data['is_record'] = 1;
        User::update($data);
        $this->successCode($data);
    }
    
    //消息通知
    public function getNews()
    {
        $data = ShopMessageTip::uid()->pages()->field('id,title,content,status,create_at time')->select();
        $this->successCode($data);
    }
    
    //设置已读
    public function getSetNews(){
        ShopMessageTip::uid()->where('status',0)->setField('status',1);
        $this->successCode();
    }
    
    //钱包明细
    public function getRecord()
    {
        $data= ShopUserMoneyRecord::uid()->field("DATE_FORMAT(create_at,'%Y-%m') months")
            ->group('months')->select();
        if ($data){
            foreach ($data as $key => $value){
                $day = ShopUserMoneyRecord::uid()->field('id,title,type,money,create_at')->where('create_at','like',$value['months'].'%')->select();
                $data[$key]['day'] = $day;
            }
        }
        $this->successCode($data);
    }

    /*
     * 学历年级列表
     */
    public function getLevel()
    {
        $list = ShopStudyCate::field('id,title as text,pid')->select()->toArray();
        $data = Arr::tree($list,0);
        $this->successCode($data);
    }

    /**
     * 职业内容
     */
    public function getIdentity()
    {
        $list = ShopMemberIdentity::field('id,identity_name')->select()->toArray();
        $this->successCode($list);
    }
}